package com.stox.core.ui.swing.util;

import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

import com.stox.core.ui.swing.widget.GlassPane;
import com.stox.design.Callback;

public class SwingUtil {

	public static Font mediumFont = new Font("Goudy Old Style",Font.PLAIN,14);
	public static Font smallFont = new Font("Goudy Old Style",Font.PLAIN,10);
	private static Rectangle screenSize;
	
	public static void initializeWidget(Component component){
		component.setFont(mediumFont);
	}
        
	public static Rectangle getScreenSize(){
		if(screenSize == null){
			 GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.
					 getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
			Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfiguration);         
	        Rectangle screenSize = graphicsConfiguration.getBounds();
	        Rectangle maxBounds = new Rectangle(screenInsets.left + screenSize.x, 
	                screenInsets.top + screenSize.y, 
	                screenSize.x + screenSize.width - screenInsets.right - screenInsets.left,
	                screenSize.y + screenSize.height - screenInsets.bottom - screenInsets.top);
	        SwingUtil.screenSize = maxBounds;
		}
		return screenSize;
	}
	
	public static Rectangle getMaxAreaRectangle(Rectangle[] rectangles){
		Rectangle result = new Rectangle();
		if(rectangles != null && rectangles.length != 0){
			for(Rectangle rectangle : rectangles){
				if(result.width*result.height < rectangle.width*rectangle.height){
					result = rectangle;
				}
			}
		}
		return result;
	}
	
	private static GlassPane glassPane;
	public static void freeze(Component component){
		if(glassPane == null){
			glassPane = new GlassPane();
		}
		
		getRootPane(component).setGlassPane(glassPane);
		glassPane.setVisible(true);
	}
	
	public static void deFreeze(Component component){
		JRootPane rootPane = getRootPane(component);
		rootPane.getGlassPane().setVisible(false);
	}
	
	public static JRootPane getRootPane(Component component){
	 		if (component instanceof JRootPane) {
	 			return (JRootPane)component;
	 		}
	 		if (component.getParent() != null) {
	 			return getRootPane(component.getParent());
	 		}
	 		
	 		// Get the window of the component.
	 		Window window = SwingUtilities.windowForComponent(component);
	 		return getRootPane(window);	
	 	}
	
	public static <T,V> void executeInFreeze(Component component, Callback<T,V> callback, T payload){
		try{
			freeze(component);
			callback.execute(payload);
		}finally{
			deFreeze(component);
		}
	}
	
	public static void restrictPoint(java.awt.Point point,Rectangle rectangle){
		if(!rectangle.contains(point)){
			if(point.x < rectangle.x)
				point.x = rectangle.x;
			if(point.y < rectangle.y)
				point.y = rectangle.y;
			if(point.x > rectangle.x+rectangle.width)
				point.x = rectangle.x+rectangle.width;
			if(point.y > rectangle.y+rectangle.height)
				point.y = rectangle.y+rectangle.height;
		}
	}
	
	
	public static void restrictPointOutside(java.awt.Point point,Rectangle rectangle){
		if(!rectangle.contains(point)){
			if(point.x < rectangle.x)
				point.x = rectangle.x - 5;
			if(point.y < rectangle.y)
				point.y = rectangle.y - 5;
			if(point.x > rectangle.x+rectangle.width)
				point.x = rectangle.x+rectangle.width + 5;
			if(point.y > rectangle.y+rectangle.height)
				point.y = rectangle.y+rectangle.height + 5;
		}
	}
	
	
	

	
}
